home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / viper-ex.el.z / viper-ex.el
Encoding:
Text File  |  1998-10-28  |  62.7 KB  |  2,010 lines

  1. ;;; viper-ex.el --- functions implementing the Ex commands for Viper
  2.  
  3. ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  19. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. ;; Boston, MA 02111-1307, USA.
  21.  
  22.  
  23. ;; Code
  24.  
  25. (require 'viper-util)
  26.  
  27. ;; Compiler pacifier
  28. (defvar read-file-name-map)
  29. ;; end compiler pacifier
  30.  
  31. ;;; Variables
  32.  
  33. (defconst vip-ex-work-buf-name " *ex-working-space*")
  34. (defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
  35. (defconst vip-ex-tmp-buf-name " *ex-tmp*")
  36.  
  37.  
  38. ;;; Variable completion in :set command
  39.   
  40. ;; The list of Ex commands. Used for completing command names.
  41. (defconst ex-token-alist
  42.   '(("!") ("=") (">") ("&") ("~")
  43.     ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual") 
  44.     ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
  45.     ("substitute") ("submitReport") ("stop")  ("sr") ("source") ("shell")
  46.     ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
  47.     ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
  48.     ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
  49.     ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
  50.     ("chdir") ("cd") ("Buffer") ("buffer") ("args"))  )
  51.  
  52. ;; A-list of Ex variables that can be set using the :set command.
  53. (defconst ex-variable-alist 
  54.   '(("wrapscan") ("ws") ("wrapmargin") ("wm")
  55.     ("global-tabstop") ("gts") ("tabstop") ("ts")
  56.     ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
  57.     ("readonly") ("ro") 
  58.     ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
  59.     ("noreadonly") ("noro") ("nomagic") ("noma")
  60.     ("noignorecase") ("noic")
  61.     ("global-noautoindent") ("gnoai") ("noautoindent") ("noai")
  62.     ("magic") ("ma") ("ignorecase") ("ic")
  63.     ("global-autoindent") ("gai") ("autoindent") ("ai")
  64.     ))
  65.  
  66.   
  67.  
  68. ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
  69. (defvar ex-token nil)
  70.  
  71. ;; Type of token. 
  72. ;; If non-nil, gives type of address; if nil, it is a command.
  73. (defvar ex-token-type nil)
  74.  
  75. ;; List of addresses passed to Ex command
  76. (defvar ex-addresses nil)
  77.  
  78. ;; It seems that this flag is used only for `#', `print', and `list', which
  79. ;; aren't implemented. Check later.
  80. (defvar ex-flag nil)
  81.  
  82. ;; "buffer" where Ex commands keep deleted data.
  83. ;; In Emacs terms, this is a register.
  84. (defvar ex-buffer nil)
  85.  
  86. ;; Value of ex count.
  87. (defvar ex-count nil)
  88.  
  89. ;; Flag for global command.
  90. (defvar ex-g-flag nil)
  91.  
  92. ;; If t, global command is executed on lines not matching ex-g-pat.
  93. (defvar ex-g-variant nil)
  94.  
  95. ;; Save reg-exp used in substitute.
  96. (defvar ex-reg-exp nil)
  97.  
  98.  
  99. ;; Replace pattern for substitute.
  100. (defvar ex-repl nil)
  101.  
  102. ;; Pattern for global command.
  103. (defvar ex-g-pat nil)
  104.  
  105.  
  106. (defvar ex-unix-type-shell
  107.   (let ((case-fold-search t))
  108.     (and (stringp shell-file-name)
  109.      (string-match
  110.       (concat
  111.        "\\("
  112.        "csh$\\|csh.exe$"
  113.        "\\|"
  114.        "ksh$\\|ksh.exe$"
  115.        "\\|"
  116.        "^sh$\\|sh.exe$"
  117.        "\\|"
  118.        "[^a-z]sh$\\|[^a-z]sh.exe$"
  119.        "\\|"
  120.        "bash$\\|bash.exe$"
  121.        "\\)")
  122.       shell-file-name)))
  123.   "Is the user using a unix-type shell?")
  124.  
  125. (defvar ex-unix-type-shell-options
  126.   (let ((case-fold-search t))
  127.     (if ex-unix-type-shell
  128.     (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name)
  129.            "-f") ; csh: do it fast
  130.           ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name)
  131.            "-noprofile") ; bash: ignore .profile
  132.           )))
  133.   "Options to pass to the Unix-style shell. 
  134. Don't put `-c' here, as it is added automatically.")
  135.  
  136. (defvar ex-nontrivial-find-file-function
  137.   (cond (ex-unix-type-shell 'vip-ex-nontrivial-find-file-unix)
  138.     ((eq system-type 'emx) 'vip-ex-nontrivial-find-file-ms) ; OS/2
  139.     (vip-ms-style-os-p 'vip-ex-nontrivial-find-file-ms) ; a Microsoft OS
  140.     (vip-vms-os-p 'vip-ex-nontrivial-find-file-unix) ; VMS
  141.     (t  'vip-ex-nontrivial-find-file-unix) ; presumably UNIX
  142.     ))
  143.  
  144. ;; Remembers the previous Ex tag.
  145. (defvar ex-tag nil)
  146.  
  147. ;; file used by Ex commands like :r, :w, :n
  148. (defvar ex-file nil)
  149.  
  150. ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
  151. (defvar ex-variant nil)
  152.  
  153. ;; Specified the offset of an Ex command, such as :read.
  154. (defvar ex-offset nil)
  155.  
  156. ;; Tells Ex that this is a w>> command.
  157. (defvar ex-append nil)
  158.  
  159. ;; File containing the shell command to be executed at Ex prompt,
  160. ;; e.g., :r !date
  161. (defvar ex-cmdfile nil)
  162.   
  163. ;; flag used in vip-ex-read-file-name to indicate that we may be reading
  164. ;; multiple file names. Used for :edit and :next
  165. (defvar vip-keep-reading-filename nil)
  166.  
  167. (defconst ex-cycle-other-window t
  168.   "*If t, :n and :b cycles through files and buffers in other window.
  169. Then :N and :B cycles in the current window. If nil, this behavior is
  170. reversed.")
  171.  
  172. (defconst ex-cycle-through-non-files nil
  173.   "*Cycle through *scratch* and other buffers that don't visit any file.")
  174.  
  175. ;; Last shell command executed with :! command.
  176. (defvar vip-ex-last-shell-com nil)
  177.   
  178. ;; Indicates if Minibuffer was exited temporarily in Ex-command.
  179. (defvar vip-incomplete-ex-cmd nil)
  180.   
  181. ;; Remembers the last ex-command prompt.
  182. (defvar vip-last-ex-prompt "")
  183.  
  184.  
  185. ;;; Code
  186.   
  187. ;; Check if ex-token is an initial segment of STR
  188. (defun vip-check-sub (str)
  189.   (let ((length (length ex-token)))
  190.     (if (and (<= length (length str))
  191.            (string= ex-token (substring str 0 length)))
  192.     (setq ex-token str)
  193.       (setq ex-token-type 'non-command))))
  194.  
  195. ;; Get a complete ex command
  196. (defun vip-get-ex-com-subr ()
  197.   (let (case-fold-search)
  198.     (set-mark (point))
  199.     (re-search-forward "[a-zA-Z][a-zA-Z]*")
  200.     (setq ex-token-type 'command)
  201.     (setq ex-token (buffer-substring (point) (mark t)))
  202.     (exchange-point-and-mark)
  203.     (cond ((looking-at "a")
  204.        (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
  205.          ((looking-at "ar") (vip-check-sub "args"))
  206.          (t (vip-check-sub "append"))))
  207.       ((looking-at "h") (vip-check-sub "help"))
  208.       ((looking-at "c")
  209.        (cond ((looking-at "cd") (vip-check-sub "cd"))
  210.          ((looking-at "ch") (vip-check-sub "chdir"))
  211.          ((looking-at "co") (vip-check-sub "copy"))
  212.          (t (vip-check-sub "change"))))
  213.       ((looking-at "d") (vip-check-sub "delete"))
  214.       ((looking-at "b") (vip-check-sub "buffer"))
  215.       ((looking-at "B") (vip-check-sub "Buffer"))
  216.       ((looking-at "e")
  217.        (if (looking-at "ex") (vip-check-sub "ex")
  218.          (vip-check-sub "edit")))
  219.       ((looking-at "f") (vip-check-sub "file"))
  220.       ((looking-at "g") (vip-check-sub "global"))
  221.       ((looking-at "i") (vip-check-sub "insert"))
  222.       ((looking-at "j") (vip-check-sub "join"))
  223.       ((looking-at "l") (vip-check-sub "list"))
  224.       ((looking-at "m")
  225.        (cond ((looking-at "map") (vip-check-sub "map"))
  226.          ((looking-at "mar") (vip-check-sub "mark"))
  227.          (t (vip-check-sub "move"))))
  228.       ((looking-at "k[a-z][^a-z]")
  229.        (setq ex-token "kmark")
  230.        (forward-char 1)
  231.        (exchange-point-and-mark))   ; this is canceled out by another
  232.                     ; exchange-point-and-mark at the end
  233.       ((looking-at "k") (vip-check-sub "kmark"))
  234.       ((looking-at "n") (if (looking-at "nu")
  235.                 (vip-check-sub "number")
  236.                   (vip-check-sub "next")))
  237.       ((looking-at "N") (vip-check-sub "Next"))
  238.       ((looking-at "o") (vip-check-sub "open"))
  239.       ((looking-at "p")
  240.        (cond ((looking-at "pre") (vip-check-sub "preserve"))
  241.          ((looking-at "pu") (vip-check-sub "put"))
  242.          ((looking-at "pw") (vip-check-sub "pwd"))
  243.          (t (vip-check-sub "print"))))
  244.       ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
  245.       ((looking-at "R") (vip-check-sub "RelatedFile"))
  246.       ((looking-at "q") (vip-check-sub "quit"))
  247.       ((looking-at "r")
  248.        (cond ((looking-at "rec") (vip-check-sub "recover"))
  249.          ((looking-at "rew") (vip-check-sub "rewind"))
  250.          (t (vip-check-sub "read"))))
  251.       ((looking-at "s")
  252.        (cond ((looking-at "se") (vip-check-sub "set"))
  253.          ((looking-at "sh") (vip-check-sub "shell"))
  254.          ((looking-at "so") (vip-check-sub "source"))
  255.          ((looking-at "sr") (vip-check-sub "sr"))
  256.          ((looking-at "st") (vip-check-sub "stop"))
  257.          ((looking-at "sus") (vip-check-sub "suspend"))
  258.          ((looking-at "subm") (vip-check-sub "submitReport"))
  259.          (t (vip-check-sub "substitute"))))
  260.       ((looking-at "t")
  261.        (if (looking-at "ta") (vip-check-sub "tag")
  262.          (vip-check-sub "transfer")))
  263.       ((looking-at "u")
  264.        (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
  265.          ((looking-at "unm") (vip-check-sub "unmap"))
  266.          (t (vip-check-sub "undo"))))
  267.       ((looking-at "v")
  268.        (cond ((looking-at "ve") (vip-check-sub "version"))
  269.          ((looking-at "vi") (vip-check-sub "visual"))
  270.          (t (vip-check-sub "vglobal"))))
  271.       ((looking-at "w")
  272.        (if (looking-at "wq") (vip-check-sub "wq")
  273.          (vip-check-sub "write")))
  274.       ((looking-at "W")
  275.        (if (looking-at "WW") 
  276.            (vip-check-sub "WWrite")
  277.          (vip-check-sub "Write")))
  278.       ((looking-at "x") (vip-check-sub "xit"))
  279.       ((looking-at "y") (vip-check-sub "yank"))
  280.       ((looking-at "z") (vip-check-sub "z")))
  281.     (exchange-point-and-mark)
  282.     ))
  283.  
  284. ;; Get an ex-token which is either an address or a command.
  285. ;; A token has a type, \(command, address, end-mark\), and a value
  286. (defun vip-get-ex-token ()
  287.   (save-window-excursion
  288.     (set-buffer vip-ex-work-buf)
  289.     (skip-chars-forward " \t|")
  290.     (cond ((looking-at "#")
  291.        (setq ex-token-type 'command)
  292.        (setq ex-token (char-to-string (following-char)))
  293.        (forward-char 1))
  294.       ((looking-at "[a-z]") (vip-get-ex-com-subr))
  295.       ((looking-at "\\.")
  296.        (forward-char 1)
  297.        (setq ex-token-type 'dot))
  298.       ((looking-at "[0-9]")
  299.        (set-mark (point))
  300.        (re-search-forward "[0-9]*")
  301.        (setq ex-token-type
  302.          (cond ((eq ex-token-type 'plus) 'add-number)
  303.                ((eq ex-token-type 'minus) 'sub-number)
  304.                (t 'abs-number)))
  305.        (setq ex-token (string-to-int (buffer-substring (point) (mark t)))))
  306.       ((looking-at "\\$")
  307.        (forward-char 1)
  308.        (setq ex-token-type 'end))
  309.       ((looking-at "%")
  310.        (forward-char 1)
  311.        (setq ex-token-type 'whole))
  312.       ((looking-at "+")
  313.        (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
  314.           (forward-char 1)
  315.           (insert "1")
  316.           (backward-char 1)
  317.           (setq ex-token-type 'plus))
  318.          ((looking-at "+[0-9]")
  319.           (forward-char 1)
  320.           (setq ex-token-type 'plus))
  321.          (t
  322.           (error vip-BadAddress))))
  323.       ((looking-at "-")
  324.        (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
  325.           (forward-char 1)
  326.           (insert "1")
  327.           (backward-char 1)
  328.           (setq ex-token-type 'minus))
  329.          ((looking-at "-[0-9]")
  330.           (forward-char 1)
  331.           (setq ex-token-type 'minus))
  332.          (t
  333.           (error vip-BadAddress))))
  334.       ((looking-at "/")
  335.        (forward-char 1)
  336.        (set-mark (point))
  337.        (let ((cont t))
  338.          (while (and (not (eolp)) cont)
  339.            ;;(re-search-forward "[^/]*/")
  340.            (re-search-forward "[^/]*\\(/\\|\n\\)")
  341.            (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
  342.            (setq cont nil))))
  343.        (backward-char 1)
  344.        (setq ex-token (buffer-substring (point) (mark t)))
  345.        (if (looking-at "/") (forward-char 1))
  346.        (setq ex-token-type 'search-forward))
  347.       ((looking-at "\\?")
  348.        (forward-char 1)
  349.        (set-mark (point))
  350.        (let ((cont t))
  351.          (while (and (not (eolp)) cont)
  352.            ;;(re-search-forward "[^\\?]*\\?")
  353.            (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
  354.            (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
  355.            (setq cont nil))
  356.            (backward-char 1)
  357.            (if (not (looking-at "\n")) (forward-char 1))))
  358.        (setq ex-token-type 'search-backward)
  359.        (setq ex-token (buffer-substring (1- (point)) (mark t))))
  360.       ((looking-at ",")
  361.        (forward-char 1)
  362.        (setq ex-token-type 'comma))
  363.       ((looking-at ";")
  364.        (forward-char 1)
  365.        (setq ex-token-type 'semi-colon))
  366.       ((looking-at "[!=><&~]")
  367.        (setq ex-token-type 'command)
  368.        (setq ex-token (char-to-string (following-char)))
  369.        (forward-char 1))
  370.       ((looking-at "'")
  371.        (setq ex-token-type 'goto-mark)
  372.        (forward-char 1)
  373.        (cond ((looking-at "'") (setq ex-token nil))
  374.          ((looking-at "[a-z]") (setq ex-token (following-char)))
  375.          (t (error "Marks are ' and a-z")))
  376.        (forward-char 1))
  377.       ((looking-at "\n")
  378.        (setq ex-token-type 'end-mark)
  379.        (setq ex-token "goto"))
  380.       (t
  381.        (error vip-BadExCommand)))))
  382.  
  383. ;; Reads Ex command. Tries to determine if it has to exit because command
  384. ;; is complete or invalid. If not, keeps reading command.
  385. (defun ex-cmd-read-exit ()
  386.   (interactive)
  387.   (setq vip-incomplete-ex-cmd t)
  388.   (let ((quit-regex1 (concat
  389.               "\\(" "set[ \t]*"
  390.               "\\|" "edit[ \t]*"
  391.               "\\|" "[nN]ext[ \t]*"
  392.               "\\|" "unm[ \t]*"
  393.               "\\|" "^[ \t]*rep"
  394.               "\\)"))
  395.     (quit-regex2 (concat
  396.               "[a-zA-Z][ \t]*"
  397.               "\\(" "!" "\\|" ">>"
  398.               "\\|" "\\+[0-9]+"
  399.               "\\)"
  400.               "*[ \t]*$"))
  401.     (stay-regex (concat
  402.              "\\(" "^[ \t]*$"
  403.              "\\|" "[?/].*[?/].*"
  404.              "\\|" "[ktgjmsz][ \t]*$"
  405.              "\\|" "^[ \t]*ab.*"
  406.              "\\|" "tr[ansfer \t]*"
  407.              "\\|" "sr[ \t]*"
  408.              "\\|" "mo.*"
  409.              "\\|" "^[ \t]*k?ma[^p]*"
  410.              "\\|" "^[ \t]*fi.*"
  411.              "\\|" "v?gl.*"
  412.              "\\|" "[vg][ \t]*$"
  413.              "\\|" "jo.*"
  414.              "\\|" "^[ \t]*ta.*"
  415.              "\\|" "^[ \t]*una.*"
  416.              "\\|" "^[ \t]*su.*"
  417.              "\\|['`][a-z][ \t]*"
  418.              "\\|" "![ \t]*[a-zA-Z].*"
  419.              "\\)"
  420.              "!*")))
  421.     
  422.     (save-window-excursion ;; put cursor at the end of the Ex working buffer
  423.       (set-buffer vip-ex-work-buf)
  424.       (goto-char (point-max)))
  425.     (cond ((vip-looking-back quit-regex1) (exit-minibuffer))
  426.       ((vip-looking-back stay-regex)  (insert " "))
  427.       ((vip-looking-back quit-regex2) (exit-minibuffer))
  428.       (t (insert " ")))))
  429.   
  430. ;; complete Ex command
  431. (defun ex-cmd-complete ()
  432.   (interactive)
  433.   (let (save-pos dist compl-list string-to-complete completion-result)
  434.     
  435.     (save-excursion
  436.       (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
  437.         save-pos (point)))
  438.     
  439.     (if (or (= dist 0)
  440.         (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
  441.         (vip-looking-back
  442.          "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
  443.     ;; Preceding characters are not the ones allowed in an Ex command
  444.     ;; or we have typed past command name.
  445.     ;; Note: we didn't do parsing, so there may be surprises.
  446.     (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
  447.         (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
  448.         (looking-at "[^ \t\n\C-m]"))
  449.         nil
  450.       (with-output-to-temp-buffer "*Completions*" 
  451.         (display-completion-list
  452.          (vip-alist-to-list ex-token-alist))))
  453.       ;; Preceding chars may be part of a command name
  454.       (setq string-to-complete (buffer-substring save-pos (point)))
  455.       (setq completion-result
  456.         (try-completion string-to-complete ex-token-alist))
  457.       
  458.       (cond ((eq completion-result t)  ; exact match--do nothing
  459.          (vip-tmp-insert-at-eob " (Sole completion)"))
  460.         ((eq completion-result nil)
  461.          (vip-tmp-insert-at-eob " (No match)"))
  462.         (t  ;; partial completion
  463.          (goto-char save-pos)
  464.          (delete-region (point) (point-max))
  465.          (insert completion-result)
  466.          (let (case-fold-search)
  467.            (setq compl-list
  468.              (vip-filter-alist (concat "^" completion-result)
  469.                        ex-token-alist)))
  470.          (if (> (length compl-list) 1)
  471.          (with-output-to-temp-buffer "*Completions*" 
  472.            (display-completion-list
  473.             (vip-alist-to-list (reverse compl-list)))))))
  474.       )))
  475.     
  476.  
  477. ;; Read Ex commands 
  478. ;; Ex commands themselves are implemented in viper-ex.el
  479. (defun vip-ex (&optional string)
  480.   (interactive)
  481.   (or string
  482.       (setq ex-g-flag nil
  483.         ex-g-variant nil))
  484.   (let* ((map (copy-keymap minibuffer-local-map))
  485.      (address nil)
  486.      (cont t)
  487.      (dot (point))
  488.      prev-token-type com-str)
  489.      
  490.     (vip-add-keymap vip-ex-cmd-map map)
  491.     
  492.     (setq com-str (or string (vip-read-string-with-history
  493.                   ":" 
  494.                   nil
  495.                   'vip-ex-history
  496.                   (car vip-ex-history)
  497.                   map)))
  498.     (save-window-excursion
  499.       ;; just a precaution
  500.       (or (vip-buffer-live-p vip-ex-work-buf)
  501.       (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name)))
  502.       (set-buffer vip-ex-work-buf)
  503.       (delete-region (point-min) (point-max))
  504.       (insert com-str "\n")
  505.       (goto-char (point-min)))
  506.     (setq ex-token-type nil
  507.       ex-addresses nil)
  508.     (while cont
  509.       (vip-get-ex-token)
  510.       (cond ((memq ex-token-type '(command end-mark))
  511.          (if address (setq ex-addresses (cons address ex-addresses)))
  512.          (cond ((string= ex-token "global")
  513.             (ex-global nil)
  514.             (setq cont nil))
  515.            ((string= ex-token "vglobal")
  516.             (ex-global t)
  517.             (setq cont nil))
  518.            (t
  519.             (vip-execute-ex-command)
  520.             (save-window-excursion
  521.               (set-buffer vip-ex-work-buf)
  522.               (skip-chars-forward " \t")
  523.               (cond ((looking-at "|")
  524.                  (forward-char 1))
  525.                 ((looking-at "\n")
  526.                  (setq cont nil))
  527.                 (t (error "`%s': %s" ex-token vip-SpuriousText)))
  528.               ))
  529.            ))
  530.         ((eq ex-token-type 'non-command)
  531.          (error "`%s': %s" ex-token vip-BadExCommand))
  532.         ((eq ex-token-type 'whole)
  533.          (setq address nil)
  534.          (setq ex-addresses
  535.            (if ex-addresses
  536.                (cons (point-max) ex-addresses)
  537.              (cons (point-max) (cons (point-min) ex-addresses)))))
  538.         ((eq ex-token-type 'comma)
  539.          (if (eq prev-token-type 'whole)
  540.          (setq address (point-min)))
  541.          (setq ex-addresses
  542.            (cons (if (null address) (point) address) ex-addresses)))
  543.         ((eq ex-token-type 'semi-colon)
  544.          (if (eq prev-token-type 'whole)
  545.          (setq address (point-min)))
  546.          (if address (setq dot address))
  547.          (setq ex-addresses
  548.            (cons (if (null address) (point) address) ex-addresses)))
  549.         (t (let ((ans (vip-get-ex-address-subr address dot)))
  550.          (if ans (setq address ans)))))
  551.       (setq prev-token-type ex-token-type))))
  552.       
  553.  
  554. ;; Get a regular expression and set `ex-variant', if found
  555. (defun vip-get-ex-pat ()
  556.   (save-window-excursion
  557.     (set-buffer vip-ex-work-buf)
  558.     (skip-chars-forward " \t")
  559.     (if (looking-at "!")
  560.     (progn
  561.       (setq ex-g-variant (not ex-g-variant)
  562.         ex-g-flag (not ex-g-flag))
  563.       (forward-char 1)
  564.       (skip-chars-forward " \t")))
  565.     (let ((c (following-char)))
  566.       (if (string-match "[0-9A-Za-z]" (format "%c" c))
  567.       (error
  568.        "Global regexp must be inside matching non-alphanumeric chars"))
  569.       (if (looking-at "[^\\\\\n]")
  570.       (progn
  571.         (forward-char 1)
  572.         (set-mark (point))
  573.         (let ((cont t))
  574.           (while (and (not (eolp)) cont)
  575.         (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
  576.             (if (member ex-token '("global" "vglobal"))
  577.             (error
  578.              "Missing closing delimiter for global regexp")
  579.               (goto-char (point-max))))
  580.         (if (not (vip-looking-back
  581.               (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
  582.             (setq cont nil))))
  583.         (setq ex-token
  584.           (if (= (mark t) (point)) ""
  585.             (buffer-substring (1- (point)) (mark t))))
  586.         (backward-char 1))
  587.     (setq ex-token nil))
  588.       c)))
  589.  
  590. ;; get an ex command
  591. (defun vip-get-ex-command ()
  592.   (save-window-excursion
  593.     (set-buffer vip-ex-work-buf)
  594.     (if (looking-at "/") (forward-char 1))
  595.     (skip-chars-forward " \t")
  596.     (cond ((looking-at "[a-z]")
  597.        (vip-get-ex-com-subr)
  598.        (if (eq ex-token-type 'non-command)
  599.            (error "`%s': %s" ex-token vip-BadExCommand)))
  600.       ((looking-at "[!=><&~]")
  601.        (setq ex-token (char-to-string (following-char)))
  602.        (forward-char 1))
  603.       (t (error vip-BadExCommand)))))
  604.  
  605. ;; Get an Ex option g or c
  606. (defun vip-get-ex-opt-gc (c)
  607.   (save-window-excursion
  608.     (set-buffer vip-ex-work-buf)
  609.     (if (looking-at (format "%c" c)) (forward-char 1))
  610.     (skip-chars-forward " \t")
  611.     (cond ((looking-at "g")
  612.        (setq ex-token "g")
  613.        (forward-char 1)
  614.        t)
  615.       ((looking-at "c")
  616.        (setq ex-token "c")
  617.        (forward-char 1)
  618.        t)
  619.       (t nil))))
  620.  
  621. ;; Compute default addresses.  WHOLE-FLAG means use the whole buffer
  622. (defun vip-default-ex-addresses (&optional whole-flag)
  623.   (cond ((null ex-addresses)
  624.      (setq ex-addresses
  625.            (if whole-flag
  626.            (cons (point-max) (cons (point-min) nil))
  627.          (cons (point) (cons (point) nil)))))
  628.     ((null (cdr ex-addresses))
  629.      (setq ex-addresses
  630.            (cons (car ex-addresses) ex-addresses)))))
  631.  
  632. ;; Get an ex-address as a marker and set ex-flag if a flag is found
  633. (defun vip-get-ex-address ()
  634.   (let ((address (point-marker)) (cont t))
  635.     (setq ex-token "")
  636.     (setq ex-flag nil)
  637.     (while cont
  638.       (vip-get-ex-token)
  639.       (cond ((eq ex-token-type 'command)
  640.          (if (member ex-token '("print" "list" "#"))
  641.          (progn
  642.            (setq ex-flag t
  643.              cont nil))
  644.            (error "Address expected in this Ex command")))
  645.         ((eq ex-token-type 'end-mark)
  646.          (setq cont nil))
  647.         ((eq ex-token-type 'whole)
  648.          (error "Trailing address expected"))
  649.         ((eq ex-token-type 'comma)
  650.          (error "`%s': %s" ex-token vip-SpuriousText))
  651.         (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
  652.          (if ans (setq address ans))))))
  653.     address))
  654.  
  655. ;; Returns an address as a point
  656. (defun vip-get-ex-address-subr (old-address dot)
  657.   (let ((address nil))
  658.     (if (null old-address) (setq old-address dot))
  659.     (cond ((eq ex-token-type 'dot)
  660.        (setq address dot))
  661.       ((eq ex-token-type 'add-number)
  662.        (save-excursion
  663.          (goto-char old-address)
  664.          (forward-line (if (= old-address 0) (1- ex-token) ex-token))
  665.          (setq address (point-marker))))
  666.       ((eq ex-token-type 'sub-number)
  667.        (save-excursion
  668.          (goto-char old-address)
  669.          (forward-line (- ex-token))
  670.          (setq address (point-marker))))
  671.       ((eq ex-token-type 'abs-number)
  672.        (save-excursion
  673.          (goto-char (point-min))
  674.          (if (= ex-token 0) (setq address 0)
  675.            (forward-line (1- ex-token))
  676.            (setq address (point-marker)))))
  677.       ((eq ex-token-type 'end)
  678.        (setq address (point-max-marker)))
  679.       ((eq ex-token-type 'plus) t)  ; do nothing
  680.       ((eq ex-token-type 'minus) t) ; do nothing
  681.       ((eq ex-token-type 'search-forward)
  682.        (save-excursion
  683.          (ex-search-address t)
  684.          (setq address (point-marker))))
  685.       ((eq ex-token-type 'search-backward)
  686.        (save-excursion
  687.          (ex-search-address nil)
  688.          (setq address (point-marker))))
  689.       ((eq ex-token-type 'goto-mark)
  690.        (save-excursion
  691.          (if (null ex-token)
  692.          (exchange-point-and-mark)
  693.            (goto-char (vip-register-to-point
  694.                (1+ (- ex-token ?a)) 'enforce-buffer)))
  695.          (setq address (point-marker)))))
  696.     address))
  697.  
  698.  
  699. ;; Search pattern and set address
  700. (defun ex-search-address (forward)
  701.   (if (string= ex-token "")
  702.       (if (null vip-s-string)
  703.       (error vip-NoPrevSearch)
  704.     (setq ex-token vip-s-string))
  705.     (setq vip-s-string ex-token))
  706.   (if forward
  707.       (progn
  708.     (forward-line 1)
  709.     (re-search-forward ex-token))
  710.     (forward-line -1)
  711.     (re-search-backward ex-token)))
  712.  
  713. ;; Get a buffer name and set `ex-count' and `ex-flag' if found
  714. (defun vip-get-ex-buffer ()
  715.   (setq ex-buffer nil)
  716.   (setq ex-count nil)
  717.   (setq ex-flag nil)
  718.   (save-window-excursion
  719.     (set-buffer vip-ex-work-buf)
  720.     (skip-chars-forward " \t")
  721.     (if (looking-at "[a-zA-Z]")
  722.     (progn
  723.       (setq ex-buffer (following-char))
  724.       (forward-char 1)
  725.       (skip-chars-forward " \t")))
  726.     (if (looking-at "[0-9]")
  727.     (progn
  728.       (set-mark (point))
  729.       (re-search-forward "[0-9][0-9]*")
  730.       (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
  731.       (skip-chars-forward " \t")))
  732.     (if (looking-at "[pl#]")
  733.     (progn
  734.       (setq ex-flag t)
  735.       (forward-char 1)))
  736.     (if (not (looking-at "[\n|]"))
  737.     (error "`%s': %s" ex-token vip-SpuriousText))))
  738.  
  739. (defun vip-get-ex-count ()
  740.   (setq ex-variant nil
  741.     ex-count nil
  742.     ex-flag nil)
  743.   (save-window-excursion
  744.     (set-buffer vip-ex-work-buf)
  745.     (skip-chars-forward " \t")
  746.     (if (looking-at "!")
  747.     (progn
  748.       (setq ex-variant t)
  749.       (forward-char 1)))
  750.     (skip-chars-forward " \t")
  751.     (if (looking-at "[0-9]")
  752.     (progn
  753.       (set-mark (point))
  754.       (re-search-forward "[0-9][0-9]*")
  755.       (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
  756.       (skip-chars-forward " \t")))
  757.     (if (looking-at "[pl#]")
  758.     (progn
  759.       (setq ex-flag t)
  760.       (forward-char 1)))
  761.     (if (not (looking-at "[\n|]"))
  762.     (error "`%s': %s"
  763.            (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand))))
  764.  
  765. ;; Expand \% and \# in ex command
  766. (defun ex-expand-filsyms (cmd buf)
  767.   (let (cf pf ret)
  768.     (save-excursion 
  769.       (set-buffer buf)
  770.       (setq cf buffer-file-name)
  771.       (setq pf (ex-next nil t))) ; this finds alternative file name
  772.     (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
  773.     (error "No current file to substitute for `%%'"))
  774.     (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
  775.     (error "No alternate file to substitute for `#'"))
  776.     (save-excursion
  777.       (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
  778.       (erase-buffer)
  779.       (insert cmd)
  780.       (goto-char (point-min))
  781.       (while (re-search-forward "%\\|#" nil t)
  782.     (let ((data (match-data)) 
  783.           (char (buffer-substring (match-beginning 0) (match-end 0))))
  784.       (if (vip-looking-back (concat "\\\\" char))
  785.           (replace-match char)
  786.         (store-match-data data)
  787.         (if (string= char "%")
  788.         (replace-match cf)
  789.           (replace-match pf)))))
  790.       (end-of-line)
  791.       (setq ret (buffer-substring (point-min) (point)))
  792.       (message "%s" ret))
  793.     ret))
  794.  
  795. ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
  796. (defun vip-get-ex-file ()
  797.   (let (prompt)
  798.     (setq ex-file nil
  799.       ex-variant nil
  800.       ex-append nil
  801.       ex-offset nil
  802.       ex-cmdfile nil)
  803.     (save-excursion
  804.       (save-window-excursion
  805.     (set-buffer vip-ex-work-buf)
  806.     (skip-chars-forward " \t")
  807.     (if (looking-at "!")
  808.         (if (and (not (vip-looking-back "[ \t]"))
  809.              ;; read doesn't have a corresponding :r! form, so ! is
  810.              ;; immediately interpreted as a shell command.
  811.              (not (string= ex-token "read")))
  812.         (progn
  813.           (setq ex-variant t)
  814.           (forward-char 1)
  815.           (skip-chars-forward " \t"))
  816.           (setq ex-cmdfile t)
  817.           (forward-char 1)
  818.           (skip-chars-forward " \t")))
  819.     (if (looking-at ">>")
  820.         (progn
  821.           (setq ex-append t
  822.             ex-variant t)
  823.           (forward-char 2)
  824.           (skip-chars-forward " \t")))
  825.     (if (looking-at "+")
  826.         (progn
  827.           (forward-char 1)
  828.           (set-mark (point))
  829.           (re-search-forward "[ \t\n]")
  830.           (backward-char 1)
  831.           (setq ex-offset (buffer-substring (point) (mark t)))
  832.           (forward-char 1)
  833.           (skip-chars-forward " \t")))
  834.     ;; this takes care of :r, :w, etc., when they get file names
  835.     ;; from the history list
  836.     (if (member ex-token '("read" "write" "edit" "visual" "next"))
  837.         (progn
  838.           (setq ex-file (buffer-substring (point)  (1- (point-max))))
  839.           (setq ex-file
  840.             ;; For :e, match multiple non-white strings separated
  841.             ;; by white. For others, find the first non-white string
  842.             (if (string-match
  843.              (if (string= ex-token "edit")
  844.                  "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
  845.                "[^ \t\n]+")
  846.              ex-file)
  847.             (progn
  848.               ;; if file name comes from history, don't leave
  849.               ;; minibuffer when the user types space
  850.               (setq vip-incomplete-ex-cmd nil)
  851.               ;; this must be the last clause in this progn
  852.               (substring ex-file (match-beginning 0) (match-end 0))
  853.               )
  854.               ""))
  855.           ;; this leaves only the command name in the work area
  856.           ;; file names are gone
  857.           (delete-region (point) (1- (point-max)))
  858.           ))
  859.     (goto-char (point-max))
  860.     (skip-chars-backward " \t\n")
  861.     (setq prompt (buffer-substring (point-min) (point)))
  862.     ))
  863.     
  864.     (setq vip-last-ex-prompt prompt)
  865.     
  866.     ;; If we just finished reading command, redisplay prompt
  867.     (if vip-incomplete-ex-cmd
  868.     (setq ex-file (vip-ex-read-file-name (format ":%s " prompt)))
  869.       ;; file was typed in-line
  870.       (setq ex-file (or ex-file "")))
  871.     ))
  872.  
  873.  
  874. ;; Completes file name or exits minibuffer. If Ex command accepts multiple
  875. ;; file names, arranges to re-enter the minibuffer.
  876. (defun vip-complete-filename-or-exit ()
  877.   (interactive)
  878.   (setq vip-keep-reading-filename t) 
  879.   ;; don't exit if directory---ex-commands don't 
  880.   (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
  881.     ;; apparently the argument to an Ex command is
  882.     ;; supposed to be a shell command
  883.     ((vip-looking-back "^[ \t]*!.*")
  884.      (setq ex-cmdfile t)
  885.      (insert " "))
  886.     (t
  887.      (setq ex-cmdfile nil)
  888.      (minibuffer-complete-word))))
  889.  
  890. (defun vip-handle-! ()
  891.   (interactive)
  892.   (if (and (string=
  893.         (buffer-string) (vip-abbreviate-file-name default-directory))
  894.        (member ex-token '("read" "write")))
  895.       (erase-buffer))
  896.   (insert "!"))
  897.  
  898. (defun ex-cmd-accepts-multiple-files-p (token)
  899.   (member token '("edit" "next" "Next")))
  900.  
  901. ;; If user doesn't enter anything, then "" is returned, i.e., the
  902. ;; prompt-directory is not returned.
  903. (defun vip-ex-read-file-name (prompt)
  904.   (let* ((str "")
  905.      (minibuffer-local-completion-map
  906.       (copy-keymap minibuffer-local-completion-map))
  907.      beg end cont val)
  908.     
  909.     (vip-add-keymap ex-read-filename-map
  910.             (if vip-emacs-p 
  911.             minibuffer-local-completion-map
  912.               read-file-name-map)) 
  913.             
  914.     (setq cont (setq vip-keep-reading-filename t))
  915.     (while cont
  916.       (setq vip-keep-reading-filename nil
  917.         val (read-file-name (concat prompt str) nil default-directory))
  918.       (if (string-match " " val)
  919.       (setq val (concat "\\\"" val "\\\"")))
  920.       (setq str  (concat str (if (equal val "") "" " ")
  921.              val (if (equal val "") "" " ")))
  922.              
  923.       ;; Only edit, next, and Next commands accept multiple files.
  924.       ;; vip-keep-reading-filename is set in the anonymous function that is
  925.       ;; bound to " " in ex-read-filename-map.
  926.       (setq cont (and vip-keep-reading-filename
  927.               (ex-cmd-accepts-multiple-files-p ex-token)))
  928.       )
  929.     
  930.     (setq beg (string-match "[^ \t]" str)   ; delete leading blanks
  931.       end (string-match "[ \t]*$" str)) ; delete trailing blanks
  932.     (if (member ex-token '("read" "write"))
  933.       (if (string-match "[\t ]*!" str)
  934.           ;; this is actually a shell command
  935.           (progn
  936.         (setq ex-cmdfile t)
  937.         (setq beg (1+ beg))
  938.         (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !")))))
  939.     (substring str (or beg 0) end)))
  940.  
  941. ;; Execute ex command using the value of addresses
  942. (defun vip-execute-ex-command ()
  943.   (vip-deactivate-mark)
  944.   (cond ((string= ex-token "args") (ex-args))
  945.     ((string= ex-token "copy") (ex-copy nil))
  946.     ((string= ex-token "cd") (ex-cd))
  947.     ((string= ex-token "chdir") (ex-cd))
  948.     ((string= ex-token "delete") (ex-delete))
  949.     ((string= ex-token "edit") (ex-edit))
  950.     ((string= ex-token "file") (vip-info-on-file))
  951.     ((string= ex-token "goto") (ex-goto))
  952.     ((string= ex-token "help") (ex-help))
  953.     ((string= ex-token "join") (ex-line "join"))
  954.     ((string= ex-token "kmark") (ex-mark))
  955.     ((string= ex-token "mark") (ex-mark))
  956.     ((string= ex-token "map") (ex-map))
  957.     ((string= ex-token "move") (ex-copy t))
  958.     ((string= ex-token "next") (ex-next ex-cycle-other-window))
  959.     ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
  960.     ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
  961.     ((string= ex-token "put") (ex-put))
  962.     ((string= ex-token "pwd") (ex-pwd))
  963.     ((string= ex-token "preserve") (ex-preserve))
  964.     ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
  965.     ((string= ex-token "quit") (ex-quit))
  966.     ((string= ex-token "read") (ex-read))
  967.     ((string= ex-token "recover") (ex-recover))
  968.     ((string= ex-token "rewind") (ex-rewind))
  969.     ((string= ex-token "submitReport") (vip-submit-report))
  970.     ((string= ex-token "set") (ex-set))
  971.     ((string= ex-token "shell") (ex-shell))
  972.     ((string= ex-token "source") (ex-source))
  973.     ((string= ex-token "sr") (ex-substitute t t))
  974.     ((string= ex-token "substitute") (ex-substitute))
  975.     ((string= ex-token "suspend") (suspend-emacs))
  976.     ((string= ex-token "stop") (suspend-emacs))
  977.     ((string= ex-token "transfer") (ex-copy nil))
  978.     ((string= ex-token "buffer") (if ex-cycle-other-window
  979.                      (vip-switch-to-buffer-other-window)
  980.                      (vip-switch-to-buffer)))
  981.     ((string= ex-token "Buffer") (if ex-cycle-other-window
  982.                      (vip-switch-to-buffer)
  983.                      (vip-switch-to-buffer-other-window)))
  984.     ((string= ex-token "tag") (ex-tag))
  985.     ((string= ex-token "undo") (vip-undo))
  986.     ((string= ex-token "unmap") (ex-unmap))
  987.     ((string= ex-token "version") (vip-version))
  988.     ((string= ex-token "visual") (ex-edit))
  989.     ((string= ex-token "write") (ex-write nil))
  990.     ((string= ex-token "Write") (save-some-buffers))
  991.     ((string= ex-token "wq") (ex-write t))
  992.     ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
  993.     ((string= ex-token "xit") (ex-write t))
  994.     ((string= ex-token "yank") (ex-yank))
  995.     ((string= ex-token "!") (ex-command))
  996.     ((string= ex-token "=") (ex-line-no))
  997.     ((string= ex-token ">") (ex-line "right"))
  998.     ((string= ex-token "<") (ex-line "left"))
  999.     ((string= ex-token "&") (ex-substitute t))
  1000.     ((string= ex-token "~") (ex-substitute t t))
  1001.     ((or (string= ex-token "append")
  1002.          (string= ex-token "change")
  1003.          (string= ex-token "insert")
  1004.          (string= ex-token "open"))
  1005.      (error "`%s': Obsolete command, not supported by Viper" ex-token))
  1006.     ((or (string= ex-token "abbreviate")
  1007.          (string= ex-token "unabbreviate"))
  1008.      (error
  1009.       "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs"
  1010.       ex-token))
  1011.     ((or (string= ex-token "list")
  1012.          (string= ex-token "print")
  1013.          (string= ex-token "z")
  1014.          (string= ex-token "#"))
  1015.      (error "`%s': Command not implemented in Viper" ex-token))
  1016.     (t (error "`%s': %s" ex-token vip-BadExCommand))))
  1017.  
  1018. (defun vip-undisplayed-files ()
  1019.   (mapcar
  1020.    (function 
  1021.     (lambda (b) 
  1022.       (if (null (get-buffer-window b))
  1023.       (let ((f (buffer-file-name b)))
  1024.         (if f f
  1025.           (if ex-cycle-through-non-files 
  1026.           (let ((s (buffer-name b)))
  1027.             (if (string= " " (substring s 0 1))
  1028.             nil
  1029.               s))
  1030.         nil)))
  1031.     nil)))
  1032.    (buffer-list)))
  1033.  
  1034.  
  1035. (defun ex-args ()
  1036.   (let ((l (vip-undisplayed-files))
  1037.     (args "")
  1038.     (file-count 1))
  1039.     (while (not (null l))
  1040.       (if (car l) 
  1041.       (setq args (format "%s %d) %s\n" args file-count (car l))
  1042.         file-count (1+ file-count)))
  1043.       (setq l (cdr l)))
  1044.     (if (string= args "")
  1045.     (message "All files are already displayed")
  1046.       (save-excursion
  1047.     (save-window-excursion
  1048.       (with-output-to-temp-buffer " *vip-info*"
  1049.         (princ "\n\nThese files are not displayed in any window.\n")
  1050.         (princ "\n=============\n")
  1051.         (princ args)
  1052.         (princ "\n=============\n")
  1053.         (princ "\nThe numbers can be given as counts to :next. ")
  1054.         (princ "\n\nPress any key to continue...\n\n"))
  1055.       (vip-read-event))))))
  1056.  
  1057. ;; Ex cd command. Default directory of this buffer changes
  1058. (defun ex-cd ()
  1059.   (vip-get-ex-file)
  1060.   (if (string= ex-file "")
  1061.       (setq ex-file "~"))
  1062.   (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
  1063.  
  1064. ;; Ex copy and move command.  DEL-FLAG means delete
  1065. (defun ex-copy (del-flag)
  1066.   (vip-default-ex-addresses)
  1067.   (let ((address (vip-get-ex-address))
  1068.     (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1069.     (goto-char end)
  1070.     (save-excursion
  1071.       (push-mark beg t)
  1072.       (vip-enlarge-region (mark t) (point))
  1073.       (if del-flag
  1074.       (kill-region (point) (mark t))
  1075.     (copy-region-as-kill (point) (mark t)))
  1076.       (if ex-flag
  1077.       (progn
  1078.         (with-output-to-temp-buffer "*copy text*"
  1079.           (princ
  1080.            (if (or del-flag ex-g-flag ex-g-variant)
  1081.            (current-kill 0)
  1082.          (buffer-substring (point) (mark t)))))
  1083.         (condition-case nil
  1084.         (progn
  1085.           (read-string "[Hit return to continue] ")
  1086.           (save-excursion (kill-buffer "*copy text*")))
  1087.           (quit (save-excursion (kill-buffer "*copy text*"))
  1088.             (signal 'quit nil))))))
  1089.     (if (= address 0)
  1090.     (goto-char (point-min))
  1091.       (goto-char address)
  1092.       (forward-line 1))
  1093.       (insert (current-kill 0))))
  1094.  
  1095. ;; Ex delete command
  1096. (defun ex-delete ()
  1097.   (vip-default-ex-addresses)
  1098.   (vip-get-ex-buffer)
  1099.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1100.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1101.     (save-excursion
  1102.       (vip-enlarge-region beg end)
  1103.       (exchange-point-and-mark)
  1104.       (if ex-count
  1105.       (progn
  1106.         (set-mark (point))
  1107.         (forward-line (1- ex-count)))
  1108.     (set-mark end))
  1109.       (vip-enlarge-region (point) (mark t))
  1110.       (if ex-flag
  1111.       ;; show text to be deleted and ask for confirmation
  1112.       (progn
  1113.         (with-output-to-temp-buffer " *delete text*"
  1114.           (princ (buffer-substring (point) (mark t))))
  1115.         (condition-case nil
  1116.         (read-string "[Hit return to continue] ")
  1117.           (quit
  1118.            (save-excursion (kill-buffer " *delete text*"))
  1119.            (error "")))
  1120.         (save-excursion (kill-buffer " *delete text*")))
  1121.     (if ex-buffer
  1122.         (cond ((vip-valid-register ex-buffer '(Letter))
  1123.            (vip-append-to-register
  1124.             (downcase ex-buffer) (point) (mark t)))
  1125.           ((vip-valid-register ex-buffer)
  1126.            (copy-to-register ex-buffer (point) (mark t) nil))
  1127.           (t (error vip-InvalidRegister ex-buffer))))
  1128.     (kill-region (point) (mark t))))))
  1129.  
  1130.  
  1131.  
  1132. ;; Ex edit command
  1133. ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
  1134. ;; asked if current buffer should really be discarded.
  1135. ;; This command can take multiple file names. It replaces the current buffer
  1136. ;; with the first file in its argument list
  1137. (defun ex-edit (&optional file)
  1138.   (if (not file)
  1139.       (vip-get-ex-file))
  1140.   (cond ((and (string= ex-file "") buffer-file-name)
  1141.      (setq ex-file  (vip-abbreviate-file-name (buffer-file-name))))
  1142.     ((string= ex-file "")
  1143.      (error vip-NoFileSpecified)))
  1144.       
  1145.   (let (msg do-edit)
  1146.     (if buffer-file-name
  1147.     (cond ((buffer-modified-p)
  1148.            (setq msg
  1149.              (format "Buffer %s is modified. Discard changes? "
  1150.                  (buffer-name))
  1151.              do-edit t))
  1152.           ((not (verify-visited-file-modtime (current-buffer)))
  1153.            (setq msg
  1154.              (format "File %s changed on disk.  Reread from disk? "
  1155.                  buffer-file-name)
  1156.              do-edit t))
  1157.           (t (setq do-edit nil))))
  1158.       
  1159.     (if do-edit
  1160.     (if (yes-or-no-p msg)
  1161.         (progn
  1162.           (set-buffer-modified-p nil)
  1163.           (kill-buffer (current-buffer)))
  1164.       (message "Buffer %s was left intact" (buffer-name))))
  1165.     ) ; let
  1166.   
  1167.   (if (null (setq file (get-file-buffer ex-file)))
  1168.       (progn 
  1169.     (ex-find-file ex-file)
  1170.     (vip-change-state-to-vi)
  1171.     (goto-char (point-min)))
  1172.     (switch-to-buffer file))
  1173.   (if ex-offset
  1174.       (progn
  1175.     (save-window-excursion
  1176.       (set-buffer vip-ex-work-buf)
  1177.       (delete-region (point-min) (point-max))
  1178.       (insert ex-offset "\n")
  1179.       (goto-char (point-min)))
  1180.     (goto-char (vip-get-ex-address))
  1181.     (beginning-of-line)))
  1182.   (ex-fixup-history vip-last-ex-prompt ex-file))
  1183.  
  1184. ;; Splits the string FILESPEC into substrings separated by newlines.
  1185. ;; Each line is assumed to be a file name. find-file's each file thus obtained.
  1186. (defun ex-find-file (filespec)
  1187.   (let ((nonstandard-filename-chars "[^a-zA-Z0-9_.-/,\\]"))
  1188.     (if (string-match nonstandard-filename-chars  filespec)
  1189.     (funcall ex-nontrivial-find-file-function filespec)
  1190.       (find-file filespec))
  1191.     ))
  1192.  
  1193.  
  1194. ;; Ex global command
  1195. (defun ex-global (variant)
  1196.   (let ((gcommand ex-token))
  1197.     (if (or ex-g-flag ex-g-variant)
  1198.     (error "`%s' within `global' is not allowed" gcommand)
  1199.       (if variant
  1200.       (setq ex-g-flag nil
  1201.         ex-g-variant t)
  1202.     (setq ex-g-flag t
  1203.           ex-g-variant nil)))
  1204.     (vip-get-ex-pat)
  1205.     (if (null ex-token)
  1206.     (error "`%s': Missing regular expression" gcommand)))
  1207.   
  1208.   (if (string= ex-token "")
  1209.       (if (null vip-s-string)
  1210.       (error vip-NoPrevSearch)
  1211.     (setq ex-g-pat vip-s-string))
  1212.     (setq ex-g-pat ex-token
  1213.       vip-s-string ex-token))
  1214.   (if (null ex-addresses)
  1215.       (setq ex-addresses (list (point-max) (point-min)))
  1216.     (vip-default-ex-addresses))
  1217.   (let ((marks nil) (mark-count 0)
  1218.     com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1219.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1220.     (save-excursion
  1221.       (vip-enlarge-region beg end)
  1222.       (exchange-point-and-mark)
  1223.       (let ((cont t) (limit (point-marker)))
  1224.     (exchange-point-and-mark)
  1225.     ;; skip the last line if empty
  1226.     (beginning-of-line)
  1227.     (if (eobp) (vip-backward-char-carefully))
  1228.     (while (and cont (not (bobp)) (>= (point) limit))
  1229.       (beginning-of-line)
  1230.       (set-mark (point))
  1231.       (end-of-line)
  1232.       (let ((found (re-search-backward ex-g-pat (mark t) t)))
  1233.         (if (or (and ex-g-flag found)
  1234.             (and ex-g-variant (not found)))
  1235.         (progn
  1236.           (end-of-line)
  1237.           (setq mark-count (1+ mark-count))
  1238.           (setq marks (cons (point-marker) marks)))))
  1239.       (beginning-of-line)
  1240.       (if (bobp) (setq cont nil)
  1241.         (forward-line -1)
  1242.         (end-of-line)))))
  1243.     (save-window-excursion
  1244.       (set-buffer vip-ex-work-buf)
  1245.       (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
  1246.     (while marks
  1247.       (goto-char (car marks))
  1248.       (vip-ex com-str)
  1249.       (setq mark-count (1- mark-count))
  1250.       (setq marks (cdr marks)))))
  1251.  
  1252. ;; Ex goto command
  1253. (defun ex-goto ()
  1254.   (if (null ex-addresses)
  1255.       (setq ex-addresses (cons (point) nil)))
  1256.   (push-mark (point) t)
  1257.   (goto-char (car ex-addresses))
  1258.   (beginning-of-line))
  1259.  
  1260. ;; Ex line commands.  COM is join, shift-right or shift-left
  1261. (defun ex-line (com)
  1262.   (vip-default-ex-addresses)
  1263.   (vip-get-ex-count)
  1264.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
  1265.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1266.     (save-excursion
  1267.       (vip-enlarge-region beg end)
  1268.       (exchange-point-and-mark)
  1269.       (if ex-count
  1270.       (progn
  1271.         (set-mark (point))
  1272.         (forward-line ex-count)))
  1273.       (if ex-flag
  1274.       ;; show text to be joined and ask for confirmation
  1275.       (progn
  1276.         (with-output-to-temp-buffer " *text*"
  1277.           (princ (buffer-substring (point) (mark t))))
  1278.         (condition-case nil
  1279.         (progn
  1280.           (read-string "[Hit return to continue] ")
  1281.           (ex-line-subr com (point) (mark t)))
  1282.           (quit (ding)))
  1283.         (save-excursion (kill-buffer " *text*")))
  1284.     (ex-line-subr com (point) (mark t)))
  1285.       (setq point (point)))
  1286.     (goto-char (1- point))
  1287.     (beginning-of-line)))
  1288.  
  1289. (defun ex-line-subr (com beg end)
  1290.   (cond ((string= com "join")
  1291.      (goto-char (min beg end))
  1292.      (while (and (not (eobp)) (< (point) (max beg end)))
  1293.        (end-of-line)
  1294.        (if (and (<= (point) (max beg end)) (not (eobp)))
  1295.            (progn
  1296.          (forward-line 1)
  1297.          (delete-region (point) (1- (point)))
  1298.          (if (not ex-variant) (fixup-whitespace))))))
  1299.     ((or (string= com "right") (string= com "left"))
  1300.      (indent-rigidly
  1301.       (min beg end) (max beg end)
  1302.       (if (string= com "right") vip-shift-width (- vip-shift-width)))
  1303.      (goto-char (max beg end))
  1304.      (end-of-line)
  1305.      (vip-forward-char-carefully))))
  1306.  
  1307.  
  1308. ;; Ex mark command
  1309. (defun ex-mark ()
  1310.   (let (char)
  1311.     (if (null ex-addresses)
  1312.     (setq ex-addresses
  1313.           (cons (point) nil)))
  1314.     (save-window-excursion
  1315.       (set-buffer vip-ex-work-buf)
  1316.       (skip-chars-forward " \t")
  1317.       (if (looking-at "[a-z]")
  1318.       (progn
  1319.         (setq char (following-char))
  1320.         (forward-char 1)
  1321.         (skip-chars-forward " \t")
  1322.         (if (not (looking-at "[\n|]"))
  1323.         (error "`%s': %s" ex-token vip-SpuriousText)))
  1324.     (error "`%s' requires a following letter" ex-token)))
  1325.     (save-excursion
  1326.       (goto-char (car ex-addresses))
  1327.       (point-to-register (1+ (- char ?a))))))
  1328.  
  1329.     
  1330.       
  1331. ;; Alternate file is the file next to the first one in the buffer ring
  1332. (defun ex-next (cycle-other-window &optional find-alt-file)
  1333.   (catch 'ex-edit
  1334.     (let (count l)
  1335.       (if (not find-alt-file) 
  1336.       (progn
  1337.         (vip-get-ex-file)
  1338.         (if (or (char-or-string-p ex-offset)
  1339.             (and (not (string= "" ex-file)) 
  1340.                  (not (string-match "^[0-9]+$" ex-file))))
  1341.         (progn
  1342.           (ex-edit t)
  1343.           (throw 'ex-edit nil))
  1344.           (setq count (string-to-int ex-file))
  1345.           (if (= count 0) (setq count 1))
  1346.           (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
  1347.     (setq count 1))
  1348.       (setq l (vip-undisplayed-files))
  1349.       (while (> count 0)
  1350.     (while (and (not (null l)) (null (car l)))
  1351.       (setq l (cdr l)))
  1352.     (setq count (1- count))
  1353.     (if (> count 0)
  1354.         (setq l (cdr l))))
  1355.       (if find-alt-file (car l)
  1356.     (progn
  1357.       (if (and (car l) (get-file-buffer (car l)))
  1358.           (let* ((w (if cycle-other-window
  1359.                 (get-lru-window) (selected-window)))
  1360.              (b (window-buffer w)))
  1361.         (set-window-buffer w (get-file-buffer (car l)))
  1362.         (bury-buffer b)
  1363.         ;; this puts "next <count>" in the ex-command history
  1364.         (ex-fixup-history vip-last-ex-prompt ex-file))
  1365.         (error "Not that many undisplayed files")))))))
  1366.  
  1367.  
  1368. (defun ex-next-related-buffer (direction &optional no-recursion)
  1369.   
  1370.   (vip-ring-rotate1 vip-related-files-and-buffers-ring direction)
  1371.   
  1372.   (let ((file-or-buffer-name 
  1373.      (vip-current-ring-item vip-related-files-and-buffers-ring))
  1374.     (old-ring vip-related-files-and-buffers-ring)
  1375.     (old-win (selected-window))
  1376.     skip-rest buf wind)
  1377.     
  1378.     (or (and (ring-p vip-related-files-and-buffers-ring)
  1379.          (> (ring-length vip-related-files-and-buffers-ring) 0))
  1380.     (error "This buffer has no related files or buffers"))
  1381.     
  1382.     (or (stringp file-or-buffer-name)
  1383.     (error
  1384.      "File and buffer names must be strings, %S" file-or-buffer-name))
  1385.     
  1386.     (setq buf (cond ((get-buffer file-or-buffer-name))
  1387.             ((file-exists-p file-or-buffer-name)
  1388.              (find-file-noselect file-or-buffer-name))
  1389.             ))
  1390.     
  1391.     (if (not (vip-buffer-live-p buf))
  1392.     (error "Didn't find buffer %S or file %S"
  1393.            file-or-buffer-name
  1394.            (vip-abbreviate-file-name
  1395.         (expand-file-name file-or-buffer-name))))
  1396.       
  1397.     (if (equal buf (current-buffer))
  1398.     (or no-recursion
  1399.         ;; try again
  1400.         (progn
  1401.           (setq skip-rest t)
  1402.           (ex-next-related-buffer direction 'norecursion))))
  1403.     
  1404.     (if skip-rest
  1405.     ()
  1406.       ;; setup buffer
  1407.       (if (setq wind (vip-get-visible-buffer-window buf))
  1408.       ()
  1409.     (setq wind (get-lru-window (if vip-xemacs-p nil 'visible)))
  1410.     (set-window-buffer wind buf))
  1411.         
  1412.       (if (vip-window-display-p)
  1413.       (progn
  1414.         (raise-frame (window-frame wind))
  1415.         (if (equal (window-frame wind) (window-frame old-win))
  1416.         (save-window-excursion (select-window wind) (sit-for 1))
  1417.           (select-window wind)))
  1418.     (save-window-excursion (select-window wind) (sit-for 1)))
  1419.     
  1420.       (save-excursion
  1421.     (set-buffer buf)
  1422.     (setq vip-related-files-and-buffers-ring old-ring))
  1423.       
  1424.       (setq vip-local-search-start-marker (point-marker))
  1425.       )))
  1426.   
  1427.     
  1428. ;; Force auto save
  1429. (defun ex-preserve ()
  1430.   (message "Autosaving all buffers that need to be saved...")
  1431.   (do-auto-save t))
  1432.  
  1433. ;; Ex put
  1434. (defun ex-put ()
  1435.   (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
  1436.     (vip-get-ex-buffer)
  1437.     (setq vip-use-register ex-buffer)
  1438.     (goto-char point)
  1439.     (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
  1440.  
  1441. ;; Ex print working directory
  1442. (defun ex-pwd ()
  1443.   (message default-directory))
  1444.  
  1445. ;; Ex quit command
  1446. (defun ex-quit ()
  1447.   ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
  1448.   (save-excursion
  1449.     (set-buffer vip-ex-work-buf)
  1450.     (if (looking-at "!") (forward-char 1)))
  1451.   (if (< vip-expert-level 3)
  1452.       (save-buffers-kill-emacs)
  1453.     (kill-buffer (current-buffer))))
  1454.  
  1455.  
  1456. ;; Ex read command
  1457. (defun ex-read ()
  1458.   (vip-get-ex-file)
  1459.   (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
  1460.     command)
  1461.     (goto-char point)
  1462.     (vip-add-newline-at-eob-if-necessary)
  1463.     (if (not (or (bobp) (eobp))) (forward-line 1))
  1464.     (if (and (not ex-variant) (string= ex-file ""))
  1465.     (progn
  1466.       (if (null buffer-file-name)
  1467.           (error vip-NoFileSpecified))
  1468.       (setq ex-file buffer-file-name)))
  1469.     (if ex-cmdfile
  1470.     (progn
  1471.       (setq command (ex-expand-filsyms ex-file (current-buffer)))
  1472.       (shell-command command t))
  1473.       (insert-file-contents ex-file)))
  1474.   (ex-fixup-history vip-last-ex-prompt ex-file))
  1475.   
  1476. ;; this function fixes ex-history for some commands like ex-read, ex-edit
  1477. (defun ex-fixup-history (&rest args)  
  1478.   (setq vip-ex-history
  1479.     (cons (mapconcat 'identity args " ") (cdr vip-ex-history))))
  1480.   
  1481.  
  1482. ;; Ex recover from emacs \#file\#
  1483. (defun ex-recover ()
  1484.   (vip-get-ex-file)
  1485.   (if (or ex-append ex-offset)
  1486.       (error "`recover': %s" vip-SpuriousText))
  1487.   (if (string= ex-file "")
  1488.       (progn
  1489.     (if (null buffer-file-name)
  1490.         (error "This buffer isn't visiting any file"))
  1491.     (setq ex-file buffer-file-name))
  1492.     (setq ex-file (expand-file-name ex-file)))
  1493.   (if (and (not (string= ex-file (buffer-file-name)))
  1494.        (buffer-modified-p)
  1495.        (not ex-variant))
  1496.       (error "No write since last change \(:rec! overrides\)"))
  1497.   (recover-file ex-file))
  1498.  
  1499. ;; Tell that `rewind' is obsolete and to use `:next count' instead
  1500. (defun ex-rewind ()
  1501.   (message
  1502.    "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
  1503.  
  1504.  
  1505. ;; read variable name for ex-set
  1506. (defun ex-set-read-variable ()
  1507.   (let ((minibuffer-local-completion-map
  1508.      (copy-keymap minibuffer-local-completion-map))
  1509.     (cursor-in-echo-area t)
  1510.     str batch)
  1511.     (define-key
  1512.       minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
  1513.     (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
  1514.     (if (vip-set-unread-command-events
  1515.      (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
  1516.     (progn
  1517.       (setq batch t)
  1518.       (vip-set-unread-command-events ?\C-m)))
  1519.     (message ":set  <Variable> [= <Value>]")
  1520.     (or batch (sit-for 2))
  1521.     
  1522.     (while (string-match "^[ \\t\\n]*$"
  1523.              (setq str
  1524.                    (completing-read ":set " ex-variable-alist)))
  1525.       (message ":set <Variable> ")
  1526.       ;; if there are unread events, don't wait
  1527.       (or (vip-set-unread-command-events "") (sit-for 2))
  1528.       ) ; while
  1529.     str))
  1530.  
  1531.  
  1532. (defun ex-set ()
  1533.   (let ((var (ex-set-read-variable))
  1534.     (val 0)
  1535.     (set-cmd "setq")
  1536.     (ask-if-save t)
  1537.     (auto-cmd-label "; don't touch or else...")
  1538.     (delete-turn-on-auto-fill-pattern
  1539.      "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
  1540.     actual-lisp-cmd lisp-cmd-del-pattern
  1541.     val2 orig-var)
  1542.     (setq orig-var var)
  1543.     (cond ((member var '("ai" "autoindent"))
  1544.        (setq var "vip-auto-indent"
  1545.          set-cmd "setq"
  1546.          ask-if-save nil
  1547.          val "t"))
  1548.       ((member var '("gai" "global-autoindent"))
  1549.        (kill-local-variable 'vip-auto-indent)
  1550.        (setq var "vip-auto-indent"
  1551.          set-cmd "setq-default"
  1552.          val "t"))
  1553.       ((member var '("noai" "noautoindent"))
  1554.        (setq var "vip-auto-indent"
  1555.          ask-if-save nil
  1556.          val "nil"))
  1557.       ((member var '("gnoai" "global-noautoindent"))
  1558.        (kill-local-variable 'vip-auto-indent)
  1559.        (setq var "vip-auto-indent"
  1560.          set-cmd "setq-default"
  1561.          val "nil"))
  1562.       ((member var '("ic" "ignorecase"))
  1563.        (setq var "vip-case-fold-search"
  1564.          val "t"))
  1565.       ((member var '("noic" "noignorecase"))
  1566.        (setq var "vip-case-fold-search"
  1567.          val "nil"))
  1568.       ((member var '("ma" "magic"))
  1569.        (setq var "vip-re-search"
  1570.          val "t"))
  1571.       ((member var '("noma" "nomagic"))
  1572.        (setq var "vip-re-search"
  1573.          val "nil"))
  1574.       ((member var '("ro" "readonly"))
  1575.        (setq var "buffer-read-only"
  1576.          val "t"))
  1577.       ((member var '("noro" "noreadonly"))
  1578.        (setq var "buffer-read-only"
  1579.          val "nil"))
  1580.       ((member var '("sm" "showmatch"))
  1581.        (setq var "blink-matching-paren"
  1582.          val "t"))
  1583.       ((member var '("nosm" "noshowmatch"))
  1584.        (setq var "blink-matching-paren"
  1585.          val "nil"))
  1586.       ((member var '("ws" "wrapscan"))
  1587.        (setq var "vip-search-wrap-around-t"
  1588.          val "t"))
  1589.       ((member var '("nows" "nowrapscan"))
  1590.        (setq var "vip-search-wrap-around-t"
  1591.          val "nil")))
  1592.     (if (eq val 0) ; value must be set by the user
  1593.     (let ((cursor-in-echo-area t))
  1594.       (message ":set %s = <Value>" var)
  1595.       ;; if there are unread events, don't wait
  1596.       (or (vip-set-unread-command-events "") (sit-for 2))
  1597.       (setq val (read-string (format ":set %s = " var)))
  1598.       (ex-fixup-history "set" orig-var val)
  1599.       
  1600.       ;; check numerical values
  1601.       (if (member var
  1602.               '("sw" "shiftwidth"
  1603.             "ts" "tabstop"
  1604.             "gts" "global-tabstop"
  1605.             "wm" "wrapmargin")) 
  1606.           (condition-case nil
  1607.           (or (numberp (setq val2 (car (read-from-string val))))
  1608.               (error "%s: Invalid value, numberp, %S" var val))
  1609.         (error
  1610.          (error "%s: Invalid value, numberp, %S" var val))))
  1611.           
  1612.       (cond
  1613.        ((member var '("sw" "shiftwidth"))
  1614.         (setq var "vip-shift-width"))
  1615.        ((member var '("ts" "tabstop"))
  1616.         ;; make it take effect in curr buff and new bufs
  1617.         (setq var "tab-width"
  1618.           set-cmd "setq"
  1619.           ask-if-save nil))
  1620.        ((member var '("gts" "global-tabstop"))
  1621.         (kill-local-variable 'tab-width)
  1622.         (setq var "tab-width"
  1623.           set-cmd "setq-default"))
  1624.        ((member var '("wm" "wrapmargin"))
  1625.         ;; make it take effect in curr buff and new bufs
  1626.         (kill-local-variable 'fill-column) 
  1627.         (setq var "fill-column" 
  1628.           val (format "(- (window-width) %s)" val)
  1629.           set-cmd "setq-default"))
  1630.        ((member var '("sh" "shell"))
  1631.         (setq var "explicit-shell-file-name"
  1632.           val (format "\"%s\"" val)))))
  1633.       (ex-fixup-history "set" orig-var))
  1634.     
  1635.     (setq actual-lisp-cmd (format "\n(%s %s %s) %s"
  1636.                   set-cmd var val auto-cmd-label))
  1637.     (setq lisp-cmd-del-pattern
  1638.       (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
  1639.           set-cmd var auto-cmd-label))
  1640.     
  1641.     (if (and ask-if-save
  1642.          (y-or-n-p (format "Do you want to save this setting in %s "
  1643.                    vip-custom-file-name)))
  1644.     (progn
  1645.       (vip-save-string-in-file 
  1646.        actual-lisp-cmd vip-custom-file-name
  1647.        ;; del pattern
  1648.        lisp-cmd-del-pattern)
  1649.       (if (string= var "fill-column")
  1650.           (if (> val2 0)
  1651.           (vip-save-string-in-file
  1652.            (concat
  1653.             "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
  1654.             auto-cmd-label)
  1655.            vip-custom-file-name
  1656.            delete-turn-on-auto-fill-pattern)
  1657.         (vip-save-string-in-file
  1658.          nil vip-custom-file-name delete-turn-on-auto-fill-pattern)
  1659.         (vip-save-string-in-file
  1660.          nil vip-custom-file-name
  1661.          ;; del pattern
  1662.          lisp-cmd-del-pattern)
  1663.         ))
  1664.       ))
  1665.     
  1666.     (message "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val)
  1667.                     (format "%S" val)
  1668.                       val))
  1669.     (eval (car (read-from-string actual-lisp-cmd)))
  1670.     (if (string= var "fill-column")
  1671.         (if (> val2 0)
  1672.             (auto-fill-mode 1)
  1673.           (auto-fill-mode -1)))
  1674.         
  1675.     ))
  1676.  
  1677. ;; In inline args, skip regex-forw and (optionally) chars-back.
  1678. ;; Optional 3d arg is a string that should replace ' ' to prevent its
  1679. ;; special meaning
  1680. (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
  1681.   (save-excursion
  1682.     (set-buffer vip-ex-work-buf)
  1683.     (goto-char (point-min))
  1684.     (re-search-forward regex-forw nil t)
  1685.     (let ((beg (point))
  1686.       end)
  1687.       (goto-char (point-max))
  1688.       (if chars-back
  1689.       (skip-chars-backward chars-back)
  1690.     (skip-chars-backward " \t\n\C-m"))
  1691.       (setq end (point))
  1692.       ;; replace SPC with `=' to suppress the special meaning SPC has
  1693.       ;; in Ex commands
  1694.       (goto-char beg)
  1695.       (if replace-str
  1696.       (while (re-search-forward " +" nil t)
  1697.         (replace-match replace-str nil t)
  1698.         (vip-forward-char-carefully)))
  1699.       (goto-char end)
  1700.       (buffer-substring beg end))))
  1701.  
  1702.  
  1703. ;; Ex shell command
  1704. (defun ex-shell ()
  1705.   (shell))
  1706.   
  1707. ;; Viper help. Invokes Info
  1708. (defun ex-help ()
  1709.   (condition-case nil
  1710.       (progn
  1711.     (pop-to-buffer (get-buffer-create "*info*"))
  1712.     (info (if vip-xemacs-p "viper.info" "viper"))
  1713.     (message "Type `i' to search for a specific topic"))
  1714.     (error (beep 1)
  1715.        (with-output-to-temp-buffer " *vip-info*"
  1716.          (princ (format "
  1717. The Info file for Viper does not seem to be installed.
  1718.  
  1719. This file is part of the standard distribution of %sEmacs.
  1720. Please contact your system administrator. "
  1721.                 (if vip-xemacs-p "X" "")
  1722.                 ))))))
  1723.  
  1724. ;; Ex source command. Loads the file specified as argument or `~/.vip'
  1725. (defun ex-source ()
  1726.   (vip-get-ex-file)
  1727.   (if (string= ex-file "")
  1728.       (load vip-custom-file-name)
  1729.     (load ex-file)))
  1730.  
  1731. ;; Ex substitute command
  1732. ;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
  1733. (defun ex-substitute (&optional repeat r-flag) 
  1734.   (let ((opt-g nil)
  1735.     (opt-c nil)
  1736.     (matched-pos nil)
  1737.     (case-fold-search vip-case-fold-search)
  1738.     delim pat repl)
  1739.     (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat)))
  1740.     (if (null ex-token)
  1741.     (progn
  1742.       (setq pat (if r-flag vip-s-string ex-reg-exp))
  1743.       (or (stringp pat)
  1744.           (error "No previous pattern to use in substitution"))
  1745.       (setq repl ex-repl
  1746.         delim (string-to-char pat)))
  1747.       (setq pat (if (string= ex-token "") vip-s-string ex-token))
  1748.       (setq vip-s-string pat
  1749.         ex-reg-exp pat)
  1750.       (setq delim (vip-get-ex-pat))
  1751.       (if (null ex-token)
  1752.       (setq ex-token ""
  1753.         ex-repl "")
  1754.     (setq repl ex-token
  1755.           ex-repl ex-token)))
  1756.     (while (vip-get-ex-opt-gc delim)
  1757.       (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
  1758.     (vip-get-ex-count)
  1759.     (if ex-count
  1760.     (save-excursion
  1761.       (if ex-addresses (goto-char (car ex-addresses)))
  1762.       (set-mark (point))
  1763.       (forward-line (1- ex-count))
  1764.       (setq ex-addresses (cons (point) (cons (mark t) nil))))
  1765.       (if (null ex-addresses)
  1766.       (setq ex-addresses (cons (point) (cons (point) nil)))
  1767.     (if (null (cdr ex-addresses))
  1768.         (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
  1769.                     ;(setq G opt-g)
  1770.     (let ((beg (car ex-addresses))
  1771.       (end (car (cdr ex-addresses)))
  1772.       eol-mark)
  1773.       (save-excursion
  1774.     (vip-enlarge-region beg end)
  1775.     (let ((limit (save-excursion
  1776.                (goto-char (max (point) (mark t)))
  1777.                (point-marker))))
  1778.       (goto-char (min (point) (mark t)))
  1779.       (while (< (point) limit)
  1780.         (end-of-line)
  1781.         (setq eol-mark (point-marker))
  1782.         (beginning-of-line)
  1783.         (if opt-g
  1784.         (progn
  1785.           (while (and (not (eolp))
  1786.                   (re-search-forward pat eol-mark t))
  1787.             (if (or (not opt-c) (y-or-n-p "Replace? "))
  1788.             (progn
  1789.               (setq matched-pos (point))
  1790.               (if (not (stringp repl))
  1791.                   (error "Can't perform Ex substitution: No previous replacement pattern"))
  1792.               (replace-match repl t))))
  1793.           (end-of-line)
  1794.           (vip-forward-char-carefully))
  1795.           (if (null pat)
  1796.           (error
  1797.            "Can't repeat Ex substitution: No previous regular expression"))
  1798.           (if (and (re-search-forward pat eol-mark t)
  1799.                (or (not opt-c) (y-or-n-p "Replace? ")))
  1800.           (progn
  1801.             (setq matched-pos (point))
  1802.             (if (not (stringp repl))
  1803.             (error "Can't perform Ex substitution: No previous replacement pattern"))
  1804.             (replace-match repl t)))
  1805.           (end-of-line)
  1806.           (vip-forward-char-carefully))))))
  1807.     (if matched-pos (goto-char matched-pos))
  1808.     (beginning-of-line)
  1809.     (if opt-c (message "done"))))
  1810.  
  1811. ;; Ex tag command
  1812. (defun ex-tag ()
  1813.   (let (tag)
  1814.     (save-window-excursion
  1815.       (set-buffer vip-ex-work-buf)
  1816.       (skip-chars-forward " \t")
  1817.       (set-mark (point))
  1818.       (skip-chars-forward "^ |\t\n")
  1819.       (setq tag (buffer-substring (mark t) (point))))
  1820.     (if (not (string= tag "")) (setq ex-tag tag))
  1821.     (vip-change-state-to-emacs)
  1822.     (condition-case conds
  1823.     (progn
  1824.       (if (string= tag "")
  1825.           (find-tag ex-tag t)
  1826.         (find-tag-other-window ex-tag))
  1827.       (vip-change-state-to-vi))
  1828.       (error
  1829.        (vip-change-state-to-vi)
  1830.        (vip-message-conditions conds)))))
  1831.  
  1832. ;; Ex write command
  1833. (defun ex-write (q-flag)
  1834.   (vip-default-ex-addresses t)
  1835.   (vip-get-ex-file)
  1836.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) 
  1837.     temp-buf writing-same-file region
  1838.     file-exists writing-whole-file)
  1839.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1840.     (if ex-cmdfile
  1841.     (progn
  1842.       (vip-enlarge-region beg end)
  1843.       (shell-command-on-region (point) (mark t) ex-file))
  1844.       (if (and (string= ex-file "") (not (buffer-file-name)))
  1845.       (setq ex-file
  1846.         (read-file-name
  1847.          (format "Buffer %s isn't visiting any file. File to save in: "
  1848.              (buffer-name)))))
  1849.       
  1850.       (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
  1851.         ex-file (if (string= ex-file "")
  1852.             (buffer-file-name)
  1853.               (expand-file-name ex-file)))
  1854.       ;; if ex-file is a directory use the file portion of the buffer file name
  1855.       (if (and (file-directory-p ex-file)
  1856.            buffer-file-name
  1857.            (not (file-directory-p buffer-file-name)))
  1858.       (setq ex-file
  1859.         (concat ex-file (file-name-nondirectory buffer-file-name))))
  1860.  
  1861.       (setq file-exists (file-exists-p ex-file)
  1862.         writing-same-file (string= ex-file (buffer-file-name)))
  1863.  
  1864.       (if (and writing-whole-file writing-same-file)
  1865.       (if (not (buffer-modified-p))
  1866.           (message "(No changes need to be saved)")
  1867.         (save-buffer)
  1868.         (ex-write-info file-exists ex-file beg end))
  1869.     ;; writing some other file or portion of the currents
  1870.     ;; file---create temp buffer for it
  1871.     ;; disable undo in that buffer, for efficiency
  1872.     (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file)))
  1873.     (unwind-protect 
  1874.         (save-excursion
  1875.           (if (and file-exists
  1876.                (not writing-same-file)
  1877.                (not (yes-or-no-p
  1878.                  (format "File %s exists. Overwrite? " ex-file))))
  1879.           (error "Quit")
  1880.         (vip-enlarge-region beg end)
  1881.         (setq region (buffer-substring (point) (mark t)))
  1882.         (set-buffer temp-buf)
  1883.         (set-visited-file-name ex-file)
  1884.         (erase-buffer)
  1885.         (if (and file-exists ex-append)
  1886.             (insert-file-contents ex-file))
  1887.         (goto-char (point-max))
  1888.         (insert region)
  1889.         (save-buffer)
  1890.         (ex-write-info file-exists ex-file (point-min) (point-max))
  1891.         )
  1892.           (set-buffer temp-buf)
  1893.           (set-buffer-modified-p nil)
  1894.           (kill-buffer temp-buf)
  1895.           ))
  1896.     )
  1897.       ;; this prevents the loss of data if writing part of the buffer
  1898.       (if (and (buffer-file-name) writing-same-file)
  1899.       (set-visited-file-modtime))
  1900.       (or writing-whole-file 
  1901.       (not writing-same-file)
  1902.       (set-buffer-modified-p t))
  1903.       (if q-flag
  1904.       (if (< vip-expert-level 2)
  1905.           (save-buffers-kill-emacs)
  1906.         (kill-buffer (current-buffer))))
  1907.       )))
  1908.       
  1909.  
  1910. (defun ex-write-info (exists file-name beg end)
  1911.   (message "`%s'%s %d lines, %d characters"
  1912.        (vip-abbreviate-file-name file-name)
  1913.        (if exists "" " [New file]")
  1914.        (count-lines beg (min (1+ end) (point-max)))
  1915.        (- end beg)))
  1916.  
  1917. ;; Ex yank command
  1918. (defun ex-yank ()
  1919.   (vip-default-ex-addresses)
  1920.   (vip-get-ex-buffer)
  1921.   (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1922.     (if (> beg end) (error vip-FirstAddrExceedsSecond))
  1923.     (save-excursion
  1924.       (vip-enlarge-region beg end)
  1925.       (exchange-point-and-mark)
  1926.       (if (or ex-g-flag ex-g-variant)
  1927.       (error "Can't execute `yank' within `global'"))
  1928.       (if ex-count
  1929.       (progn
  1930.         (set-mark (point))
  1931.         (forward-line (1- ex-count)))
  1932.     (set-mark end))
  1933.       (vip-enlarge-region (point) (mark t))
  1934.       (if ex-flag (error "`yank': %s" vip-SpuriousText))
  1935.       (if ex-buffer
  1936.       (cond ((vip-valid-register ex-buffer '(Letter))
  1937.          (vip-append-to-register
  1938.           (downcase ex-buffer) (point) (mark t)))
  1939.         ((vip-valid-register ex-buffer)
  1940.          (copy-to-register ex-buffer (point) (mark t) nil))
  1941.         (t (error vip-InvalidRegister ex-buffer))))
  1942.       (copy-region-as-kill (point) (mark t)))))
  1943.  
  1944. ;; Execute shell command
  1945. (defun ex-command ()
  1946.   (let (command)
  1947.     (save-window-excursion
  1948.       (set-buffer vip-ex-work-buf)
  1949.       (skip-chars-forward " \t")
  1950.       (setq command (buffer-substring (point) (point-max)))
  1951.       (end-of-line))
  1952.     (setq command (ex-expand-filsyms command (current-buffer)))
  1953.     (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
  1954.     (if vip-ex-last-shell-com
  1955.         (setq command (concat vip-ex-last-shell-com (substring command 1)))
  1956.       (error "No previous shell command")))
  1957.     (setq vip-ex-last-shell-com command)
  1958.     (if (null ex-addresses)
  1959.     (shell-command command)
  1960.       (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  1961.     (if (null beg) (setq beg end))
  1962.     (save-excursion
  1963.       (goto-char beg)
  1964.       (set-mark end)
  1965.       (vip-enlarge-region (point) (mark t))
  1966.       (shell-command-on-region (point) (mark t) command t))
  1967.     (goto-char beg)))))
  1968.  
  1969. ;; Print line number
  1970. (defun ex-line-no ()
  1971.   (message "%d"
  1972.        (1+ (count-lines
  1973.         (point-min)
  1974.         (if (null ex-addresses) (point-max) (car ex-addresses))))))
  1975.  
  1976. ;; Give information on the file visited by the current buffer
  1977. (defun vip-info-on-file ()
  1978.   (interactive)
  1979.   (let ((pos1 (vip-line-pos 'start))
  1980.     (pos2 (vip-line-pos 'end))
  1981.     lines file info)
  1982.     (setq lines (count-lines (point-min) (vip-line-pos 'end))
  1983.       file (if (buffer-file-name)
  1984.            (concat (vip-abbreviate-file-name (buffer-file-name)) ":")
  1985.          (concat (buffer-name) " [Not visiting any file]:"))
  1986.       info (format "line=%d/%d pos=%d/%d col=%d %s"
  1987.                (if (= pos1 pos2)
  1988.                (1+ lines)
  1989.              lines)
  1990.                (count-lines (point-min) (point-max))
  1991.                (point) (1- (point-max))
  1992.                (1+ (current-column))
  1993.                (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
  1994.     (if (< (+ 1 (length info) (length file))
  1995.        (window-width (minibuffer-window)))
  1996.     (message (concat file " " info))
  1997.       (save-window-excursion
  1998.     (with-output-to-temp-buffer " *vip-info*"
  1999.       (princ (concat "\n"
  2000.              file "\n\n\t" info
  2001.              "\n\n\nPress any key to continue...\n\n")))
  2002.     (vip-read-event)
  2003.     (kill-buffer " *vip-info*")))
  2004.     ))
  2005.  
  2006.  
  2007. (provide 'viper-ex)
  2008.  
  2009. ;;;  viper-ex.el ends here
  2010.